package com.chapter10.demo1;

import java.util.Arrays;


/**
 * 换成
 * 了Arrays类中的数组复制copyOf方法：复制了对象的引用，并没有复制实际的对象。
 */
public class ArrayCopy2 {

    public static void main(String[] args) {
        int[] arr1 = new int[]{1,2,3,4,5};
        System.out.println(Arrays.toString(arr1));  //[1, 2, 3, 4, 5]

        int[] arr2 = Arrays.copyOf(arr1,arr1.length);
        arr2[0]=10;
        System.out.println(Arrays.toString(arr2));  //[10, 2, 3, 4, 5]

        int[] arr3 = Arrays.copyOf(arr1,arr1.length);
        System.out.println(Arrays.toString(arr3)); //[1, 2, 3, 4, 5]



        Foo[] objectArr1 = new Foo[5];
        for(int i=0;i<5;i++){
            objectArr1[i] = new Foo(i);
        }
        Foo[] objectArr2 = Arrays.copyOf(objectArr1,objectArr1.length);
        objectArr2[0].value=10;
        System.out.println(objectArr1[0].value); //10

    }
}
